package com.systop.party05.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.systop.party05.R;
import com.systop.party05.base.BaseActivity;
import com.systop.party05.entity.LoginEntity;
import com.systop.party05.http.MyStringCallback;
import com.systop.party05.utils.GeneralTools;
import com.systop.party05.utils.HashUtils;
import com.systop.party05.utils.SPUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.systop.party05.http.ComUrl.LOGIN_ACOUNT;
import static com.systop.party05.utils.StaticDateUtils.SP_DYID;
import static com.systop.party05.utils.StaticDateUtils.SP_TOKEN;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.login_edt_phone)
    EditText loginEdtPhone;
    @BindView(R.id.login_edt_psw)
    EditText loginEdtPsw;
    @BindView(R.id.login_tv_login)
    TextView loginTvLogin;
    @BindView(R.id.login_tv_forget_psw)
    TextView loginTvForgetPsw;
    @BindView(R.id.login_tv_register)
    TextView loginTvRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.login_tv_login, R.id.login_tv_forget_psw, R.id.login_tv_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_tv_login:
                String phone = loginEdtPhone.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    showMessage(loginEdtPhone.getHint().toString());
                } else if (phone.length() != 11) {
                    showMessage(getString(R.string.phone_error));
                } else if (TextUtils.isEmpty(loginEdtPsw.getText())) {
                    showMessage(loginEdtPsw.getHint().toString());
                } else {
                    //登陆接口
                    //showMessage("加密后的密码:"+HashUtils.getSHA256Hash(loginEdtPsw.getText().toString().trim()));
                    okhttpLogin();
                }
                break;
            case R.id.login_tv_forget_psw://忘记密码
                break;
            case R.id.login_tv_register:  //注册
                 startActivity(new Intent(this,RegisterActivity.class));
                break;
        }
    }

    private void okhttpLogin() {
        HashMap<String, String> param = new HashMap<>();
        param.put("client", GeneralTools.getDeviceId(this));
        param.put("phone", loginEdtPhone.getText().toString().trim());
//        param.put("password", MD5Utils.MD5(loginEdtPsw.getText().toString().trim()));
        param.put("password", HashUtils.getSHA256Hash(loginEdtPsw.getText().toString().trim()));
        //cdac762d0ba79875489f6a8b430fa8b5dfe0cdd81da38b80f02f33328af7fd4a 这是123456加密后的密码
        //9ec9750e709431dad22365cabc5c625482e574c74adaebba7dd02f1129e4ce1d  这是admin加密后的密码
        //9ec9750e709431dad22365cabc5c625482e574c74adaebba7dd02f1129e4ce1d  服务端数据库存储的密码
        commonOkhttp.execute(LOGIN_ACOUNT, param, new MyStringCallback(this) {
            @Override
            public void onSuccess(String response) {
                super.onSuccess(response);
                LoginEntity entity = gson.fromJson(response, LoginEntity.class);
                showMessage(entity.getMsg());
                if (entity != null && !TextUtils.isEmpty(entity.getAccess_token())) {
                    SPUtils.savePreference(LoginActivity.this, SP_TOKEN, entity.getAccess_token());
                    SPUtils.savePreference(LoginActivity.this, SP_DYID, entity.getDyId()+"");
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finishActivity();
                }
            }
        });
    }
}